var newsPublish = new XFApp.client();
var ue = UE.getEditor('container', {
	toolbars : toolbars
});
$.validator.addMethod("byteRangeLength",function(value,element,params){
	var length = value.length;
	for(var i = 0; i < value.length; i++){
		if(value.charCodeAt(i) > 127){
			length++;
		}
	}
	return this.optional(element) || length <= 400;
	}, "最多输入200个字");

XFApp.client.prototype.newsPublish = {
	successCode : "10000",

	formValidate : function(){
		
		$("#editForm").validate({
			rules: {
				title: {
					required: true
				},
				author: {
					required: true
				},
				source: {
					required: true
				},
				publishTime: {
					required: true
				},
				newsIntro: {
					byteRangeLength: true
				}
			},
			messages: {
				title: {
					required: "请输入标题"
				},
				author: {
					required: "请输入作者"
				},
				source: {
					required: "请输入文章来源"
				},
				publishTime: {
					required: "请选择发布时间 "
				},
				newsIntro: {
					byteRangeLength: "最多输入200个字"
				}
			}
		});
		
	},
	
	submitNews : function(type) {
		var content = ue.getContent();
		var params = {
			content : content
		};
		//截取保存第一个图片地址
		var reg = /src=([\"|']?)([^\"'>]+\.(gif|jpg|jpeg|bmp|png))/i;
		var regStr = content.match(reg)
		if(regStr != null){
			params["imageUrl"] = regStr[0].substring(regStr[0].indexOf('/webshare'));
		}
		$("input").each(function() {
			if(!!$(this).attr("name")){
				if($(this).attr("type")=="radio"){
					if(!!$(this).prop("checked")){
						params[$(this).attr("name")] = $(this).val();
					}
				}else{
					params[$(this).attr("name")] = $(this).val();
				}
			}
		});
		$("select").each(function() {
			if(!!$(this).attr("name"))
				params[$(this).attr("name")] = $(this).val();
		});
		$("textarea").each(function(){
			if(!!$(this).attr("name"))
				params[$(this).attr("name")] = $(this).val();
		});
		if(type==2){
			params["id"]=null;
		}
		webDialogUtil.simpleConfrimAlert("是否确认提交", function(){
			$.ajax({
				url : basePath + "/article/save",
				data : params,
				dataType : 'json',
				type : 'post',
				success : function(data) {
					if(data.code!=10000){
						webDialogUtil.simpleAlert(data.msg, "温馨提示");
					}else{
						webDialogUtil.simpleAlert("处理成功", "温馨提示");
						if(!!data.result){
							$("input[name='id']").val(data.result.id);
							$("#mod_btn").show();
							}
						}
					}
			});
		}, null, "温馨提示");
		
	},
	
	preview : function() {
		var content = ue.getContent();
		var params = {
			content : content
		};
		$("input").each(function() {
			if(!!$(this).attr("name")){
				if($(this).attr("type")=="radio"){
					if(!!$(this).prop("checked")){
						params[$(this).attr("name")] = $(this).val();
					}
				}else{
					params[$(this).attr("name")] = $(this).val();
				}
			}
		});
		$("select").each(function() {
			if(!!$(this).attr("name"))
				params[$(this).attr("name")] = $(this).val();
		});
		$("textarea").each(function(){
			if(!!$(this).attr("name"))
				params[$(this).attr("name")] = $(this).val();
		});
		var view_form = $("<form id='view_form' style='display:none' target='_blank'></form>")
        view_form.attr('action',basePath+"/article/preview")
        view_form.attr('method','post')
        for(var key in params){
        	var input = $("<input type='hidden' name='"+key+"' value='"+params[key]+"'/>");
        	view_form.append(input);
        }
		view_form.appendTo(document.body).submit();   
		$("#view_form").remove();
	},
	event : function() {
		_this = this;
		_this.formValidate();
		$("#submit_btn").on("click",function(){
			if($("#editForm").validate().form()){
				_this.submitNews(2);
			}
		});
		$("#mod_btn").on("click",function(){
			if($("#editForm").validate().form()){
				_this.submitNews(1);
			}
		});
		$("#preview_btn").on("click",function(){
			if($("#editForm").validate().form()){
				_this.preview();
			}
		});
	},
};
$(function() {
	if(!!$("input[name='id']").val()){
		$("#mod_btn").show();
		$("#submit_btn").hide();
	}
	/**
	 * 职能部门用户，部门不可选
	 */
	if(!!$("#departmentName").val()){
		$("#departmentId>option").each(function(){
			if($(this).text().trim()==$("#departmentName").val()){
				$("#departmentId").val();
				$(this).attr("selected","selected");
				$("#departmentId").attr("disabled","disabled");
			}
		});
	}
	
	newsPublish.newsPublish.event();
	ue.addListener("ready", function () {
        // editor准备好之后才可以使用
		if(!!$("#edit_content").html()){
			 ue.setContent($("#edit_content").html());
		}
    });
});